import 'package:color_dart/RgbaColor.dart';
import 'package:flutter/material.dart';
import 'package:im_flutter_sdk_example/components/empty_list.dart';
import 'package:im_flutter_sdk_example/jsonserialize/about/data.dart';
import 'package:im_flutter_sdk_example/utils/global.dart';
import 'package:package_info/package_info.dart';
import 'package:url_launcher/url_launcher.dart';

class About extends StatefulWidget {
  @override
  _AboutState createState() => _AboutState();
}

class _AboutState extends State<About> {
  String appVersion = '';
  AboutModel aboutModel;

  @override
  void initState() {
    super.initState();
    Future.delayed(Duration.zero, () {
      _version();
      _loadAbout();
    });
  }

  _version() async {
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    appVersion = packageInfo.version;
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('关于我们')),
      body: aboutModel == null
          ? (EmptyList())
          : SingleChildScrollView(
              child: Container(
                margin: EdgeInsets.symmetric(vertical: 40, horizontal: 20),
                height: G.screenHeight() * 0.8,
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Column(
                      children: [
                        Padding(
                          padding: const EdgeInsets.only(bottom: 40),
                          child: Image.asset('lib/assets/images/logo.png',
                              height: 120, fit: BoxFit.contain),
                        ),
                        Text('${aboutModel.summary}',
                            style: TextStyle(
                                color: rgba(51, 51, 51, 1),
                                fontSize: 14,
                                wordSpacing: 2.0,
                                letterSpacing: 2.0)),
                      ],
                    ),
                    Padding(
                      padding: const EdgeInsets.only(bottom: 20),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text('官方地址：${aboutModel.website}',
                              style: TextStyle(
                                  color: rgba(165, 173, 185, 1), fontSize: 11)),
                          GestureDetector(
                            onTap: () => launch("tel://21213123123"),
                            child: Padding(
                              padding: const EdgeInsets.symmetric(vertical: 10),
                              child: Text.rich(TextSpan(children: [
                                TextSpan(
                                    text: '客服电话 :',
                                    style: TextStyle(
                                        color: rgba(165, 173, 185, 1),
                                        fontSize: 11)),
                                TextSpan(
                                    text: '${aboutModel.contact}',
                                    style: TextStyle(
                                        color: rgba(42, 125, 250, 1),
                                        fontSize: 11))
                              ])),
                            ),
                          ),
                          Container(
                              color: rgba(238, 240, 242, 1),
                              height: 1,
                              margin: EdgeInsets.symmetric(vertical: 25)),
                          Center(
                            child: Text('版本 V$appVersion',
                                textAlign: TextAlign.center,
                                style: TextStyle(
                                    color: rgba(165, 173, 185, 1),
                                    fontSize: 11)),
                          )
                        ],
                      ),
                    )
                  ],
                ),
              ),
            ),
    );
  }

  void _loadAbout() async {
    var response = await G.req.app.aboutInfo();
    aboutModel = AboutModel.fromJson(response.data['data']);
    setState(() {});
  }
}
